#include<iostream>
#include<fstream>
#include<contacts.pb.h>

using namespace std;

void AddPeopleInfo(contacts::PeopleInfo* people)
{
    cout << "------------新增联系人-----------" << endl;
    cout << "请输入联系人姓名：";
    string name;
    getline(cin,name);
    people->set_name(name);
    cout << "请输入联系人年龄：";
    int age;
    cin >> age;
    people->set_age(age);
    cin.ignore(256,'\n');//清除缓冲区的内容比如上次输入留下的换行符(遇到\n 或 达到256字节)

    for(int i = 0;;i++)
    {
        cout << "请输入联系人电话" << i+1 << "(只输入回车结束添加)";
        string number;
        getline(cin,number);
        if(number.empty()){
            break;
        }

        contacts::PeopleInfo_Phone* phone = people->add_phone();
        phone->set_number(number);
        cout << "请输入联系人电话类型(1.MP 2.TEL):" << i+1 << "(只输入回车结束添加)";
        int type;
        cin >> type;
        cin.ignore(256,'\n');
        switch(type){
            case 1:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout << "选择有误！" << endl;
                break;
        }
    }
    contacts::Address address;
    cout << "请输入家庭地址：";
    string home_address;
    getline(cin,home_address);
    address.set_home_address(home_address);
    cout << "请输入单位地址：";
    string unit_address;
    getline(cin,unit_address);
    address.set_unit_address(unit_address);

    people->mutable_data()->PackFrom(address);

    cout << "请输入其他联系方式(1.QQ 2.WECHAT):";
    int other_contacts;
    cin >> other_contacts;
    cin.ignore(256,'\n');
    if(1 == other_contacts)
    {
        cout << "请输入qq号：";
        string qq;
        getline(cin,qq);
        people->set_qq(qq);
    }
    else if(other_contacts == 2)
    {
        cout << "请输入微信号：";
        string wechat;
        getline(cin,wechat);
        people->set_wechat(wechat);
    }
    else{
        cout << "选择错误" << endl;
    }

    for(int i = 0;;i++)
    {
        cout << "请输入备注" << i+1 << "标题（只输入 回车完成备注新增）：";
        string remark_key;
        getline(cin,remark_key);
        if(remark_key.empty()){
            break;
        }
        cout << "请输入备注" << i+1 << "内容：";
        string remark_value;
        getline(cin,remark_value);
        people->mutable_remark()->insert({remark_key,remark_value});



    }
    
    cout << "------------添加联系人成功-----------" << endl;

}

int main()
{
    contacts::Contacts contacts;
    //读取本地已存在的通讯录文件
    fstream input("contacts.bin",ios::in | ios::binary);
    if(!input)
    {
        cout << "contacts.bin not find,creat new file!" << endl;
    }else if(!contacts.ParseFromIstream(&input)){
        cerr << "parse error!" << endl;
        input.close();
        return -1;
    }
    //向通讯录添加联系人
    AddPeopleInfo(contacts.add_contacts());

    //身份信息写入本地

    fstream output("contacts.bin",ios::out|ios::trunc|ios::binary);
    if(!contacts.SerializeToOstream(&output)){
        cerr << "write error!" << endl;
        input.close();
        output.close();
        return -1;
    }

    cout << "write success!" << endl;

}